package com.qf.agro.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.agro.common.ResultInfo;
import com.qf.agro.pojo.SysUser;
import com.qf.agro.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * ProjectName: smart-agriculture-parent
 * ClassName: SysUserController
 * Author: chenyaosen
 * Date: 2023/6/29 16:37
 * Description:
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController {

    @Autowired
    private SysUserService userService;

    @RequestMapping("/login")
    public ResultInfo login(@RequestBody Map<String,String> map) {
        String username = map.get("username");
        String password = map.get("password");
        return userService.login(username, password);
    }

    @GetMapping("/page")
    public ResultInfo findPage(@RequestParam(value = "page",defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "5") Integer pageSize,
                               String name) {
        Page<SysUser> page = userService.findPage(pageNum, pageSize, name);
        return ResultInfo.success(page);
    }

    @PostMapping
    public ResultInfo save(@RequestBody SysUser sysUser) {
        userService.save(sysUser);
        return ResultInfo.success(null);
    }

    @PostMapping("/checkUsername")
    public ResultInfo checkUsername(@RequestBody Map<String,String> map) {
        String username = map.get("username");
        boolean b = userService.checkUsername(username);
        if (b) {
            return ResultInfo.success(null);
        } else {
            return ResultInfo.error("");
        }
    }

    @DeleteMapping
    public ResultInfo delete(Long id) {
        userService.disabled(id);
        return ResultInfo.success(null);
    }

    @PutMapping
    public ResultInfo update(@RequestBody SysUser sysUser) {
        userService.update(sysUser);
        return ResultInfo.success(null);
    }
}


